﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using CMN.Controllers;
using CMN.Controllers.Filters;
using CMN.Common;
using System.Text;
using CMN.UI;
using CMN.Framework;
using CMN.Config;
namespace CMN.Controllers
{
    
    [LoggerFilter()]
    [ExceptionFilter()]
    [ExecutionTimingAttribute()]
    public class ColumnController : Controller
    {
        [CompressFilter]
        public string Index(string channel, string channel2,string type)
        {

           
            string path = channel;
            PageHelperEntity pageHelper = new PageHelperEntity();
            if (!string.IsNullOrEmpty(type))
            {
                if (type == "wap")
                    pageHelper.TType = TemplateType.Phone;
            }
            Normal_ChannelEntity channelEntity = MvcWebCacheManager.GetNormal_ChannelByDir(channel);
            if (channelEntity == null)
            {
                AppLog.Write("channelEntity404:" + channel, AppLog.LogMessageType.Debug);
                Utils.Redirect404();
                return "";
            }
           
            string[] strparam = channel2.Split('_');
            string channelDir = string.Empty;
          
            
                switch (strparam.Length)
                {
                    case 1:
                        {
                            channelDir = channel2;
                            break;
                        }

                    case 2:
                        {
                            channelDir = strparam[0];
                            pageHelper.PageIndex = Utils.StringToInt(strparam[1], 1);
                            break;
                        }
                    case 3:
                        {
                            channelDir = strparam[0];
                            pageHelper.PageIndex = Utils.StringToInt(strparam[1], 1);
                            pageHelper.Order = strparam[2];
                            break;
                        }
                }
            
            Normal_ChannelEntity channel2Entity = MvcWebCacheManager.GetNormal_ChannelByDir(channelDir);


            if (channel2Entity == null)
            {
                AppLog.Write("channel2Entity404:"+channelDir, AppLog.LogMessageType.Debug);
                Utils.Redirect404();
                return "";
            }
            else
            {
                pageHelper.Channel = channel2Entity;
                pageHelper.ChannelId = channel2Entity.Id;
                pageHelper.Title = (string.IsNullOrEmpty(channel2Entity.SEOTitle) ? channel2Entity.Title : channel2Entity.SEOTitle);
                pageHelper.Description = (string.IsNullOrEmpty(channel2Entity.HtmlInfo) ? channel2Entity.Info : channel2Entity.HtmlInfo); 
                pageHelper.Keywords = channel2Entity.KeyWords;
                pageHelper.Url = channel2Entity.Url;
                if (string.IsNullOrEmpty(channel))
                {


                    if (pageHelper.Channel.ReId == 0)
                    {
                        pageHelper.ParentDir = pageHelper.Channel.Dir;
                        pageHelper.ParentChannelId = pageHelper.Channel.Id;
                        pageHelper.ParentChannel = pageHelper.Channel;
                    }
                    else
                    {
                        Normal_ChannelEntity parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(pageHelper.Channel.ReId);
                        if (parentChannelEntity.ReId == 0)
                        {
                            pageHelper.ParentDir = parentChannelEntity.Dir;
                            pageHelper.ParentChannelId = parentChannelEntity.Id;
                            pageHelper.ParentChannel = parentChannelEntity;
                        }
                        else
                        {
                            parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(parentChannelEntity.ReId);
                            pageHelper.ParentDir = parentChannelEntity.Dir;
                            pageHelper.ParentChannelId = parentChannelEntity.Id;
                            pageHelper.ParentChannel = parentChannelEntity;
                        }
                    }
                }
                else
                {
                    Normal_ChannelEntity parentChannelEntity = MvcWebCacheManager.GetNormal_ChannelByDir(channel);
                    //以前有些地址改变了
                    if (parentChannelEntity != null)
                    {
                        pageHelper.ParentDir = parentChannelEntity.Dir;
                        pageHelper.ParentChannelId = parentChannelEntity.Id;
                        pageHelper.ParentChannel = parentChannelEntity;
                    }
                    else
                    {
                        if (pageHelper.Channel.ReId == 0)
                        {
                            pageHelper.ParentDir = pageHelper.Channel.Dir;
                            pageHelper.ParentChannelId = pageHelper.Channel.Id;
                            pageHelper.ParentChannel = parentChannelEntity;
                        }
                        else
                        {
                             parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(pageHelper.Channel.ReId);
                            if (parentChannelEntity.ReId == 0)
                            {
                                pageHelper.ParentDir = parentChannelEntity.Dir;
                                pageHelper.ParentChannelId = parentChannelEntity.Id;
                                pageHelper.ParentChannel = parentChannelEntity;
                            }
                            else
                            {
                                parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(parentChannelEntity.ReId);
                                pageHelper.ParentDir = parentChannelEntity.Dir;
                                pageHelper.ParentChannelId = parentChannelEntity.Id;
                                pageHelper.ParentChannel = parentChannelEntity;
                            }
                        }
                    }
                }
                if (channel2Entity.IsPart == 0)
                {
                    pageHelper.Type = PageType.ChannelsPage;
                }
                else if (channel2Entity.IsPart == 1)
                {
                    pageHelper.Type = PageType.ChannelPage;
                }
               
                BaseTemplateDisplay articleTemplate = new BaseTemplateDisplay(pageHelper);
                
                string strRetrun = articleTemplate.Display();
                articleTemplate.Dispose();
               
                return strRetrun;
            }
            return "";
           
        }



        
    }
}
